package com.songshuai.pangu.api.component;

import com.alibaba.fastjson.JSONObject;
import com.songshuai.pangu.api.controller.BaseController;
import com.songshuai.webapi.baseapi.dto.feign.request.pangu.salemeeting.QueryCustomersReqDto;
import com.songshuai.webapi.baseapi.dto.feign.request.pangu.salemeeting.SendInvitationReqDto;
import com.songshuai.webapi.baseapi.dto.feign.request.pangu.salemeeting.TalkNotesReqDto;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * 对接盘古-销售会议
 * @Author: lhx
 * @Date: 2019/3/29 19:54
 */
@Slf4j
@Component
public class SalesMeetingComponent extends BaseController {


    //查看客户回执单信息
    @Value("${pangu.sales.queryReceiptByInviteNo}")
    private String queryReceiptByInviteNoUrl;

    //查看邀请函
    @Value("${pangu.sales.queryMeetingInviteList}")
    private String queryMeetingInviteListUrl;

    //查询会议列表
    @Value("${pangu.sales.saleMettingGet}")
    private String saleMettingGetUrl;

    //查询已邀约客户列表
    @Value("${pangu.sales.saleCustomerGet}")
    private String saleCustomerGetUrl;

    //查询客户信息
    @Value("${pangu.sales.getSaleCustomerList}")
    private String getSaleCustomerListUrl;

    //发送邀请函
    @Value("${pangu.sales.addInviteInfo}")
    private String addInviteInfoUrl;

    //查询销售人员信息
    @Value("${pangu.sales.querySalesInfo}")
    private String querySalesInfoUrl;

    //转交客户
    @Value("${pangu.sales.handOverCustomer}")
    private String handOverCustomerUrl;


    //新增沟通记录
    @Value("${pangu.sales.addTalkNotes}")
    private String addTalkNotesUrl;


    //获取客户历史沟通记录
    @Value("${pangu.sales.getCommunicateList}")
    private String getCommunicateListUrl;


    /**
     * 查看客户回执单信息
     * @param inviteNo 邀约编号
     * @return
     */
    public String queryReceipt(String inviteNo){
        try {
            JSONObject object = new JSONObject();
            object.put("inviteNo",inviteNo);
            return callPanguApi(object,queryReceiptByInviteNoUrl);
        } catch (Exception e) {
            log.error("请求盘古-查看客户回执单信息异常",e);
        }
        return null;
    }


    /**
     * 查看邀请函
     * @param meetingCode
     * @return
     */
    public String queryInvitationLetter(String meetingCode){
        try {
            JSONObject object = new JSONObject();
            object.put("meetingCode",meetingCode);
            return callPanguApi(object,queryMeetingInviteListUrl);
        } catch (Exception e) {
            log.error("请求盘古-查看邀请函异常",e);
        }
        return null;
    }


    /**
     * 查询会议列表
     * @param dateType 0-最近 1-未来
     * @param saleId  销售id
     * @return
     */
    public String queryMeetingList(Integer dateType,Integer saleId){
        try {
            JSONObject object = new JSONObject();
            object.put("DateType",dateType);
            object.put("SaleId",saleId);
            return callPanguApi(object,saleMettingGetUrl);
        } catch (Exception e) {
            log.error("请求盘古-查询会议列表异常",e);
        }
        return null;
    }


    /**
     *  根据会议查询客户集合（已邀约）
     * @param saleId  销售id
     * @param meetingNo 会议编号
     * @return
     */
    public String queryCustomerList(Integer saleId,String meetingNo){
        try {
            JSONObject object = new JSONObject();
            object.put("SaleId",saleId);
            object.put("MeetingNo",meetingNo);
            return callPanguApi(object,saleCustomerGetUrl);
        } catch (Exception e) {
            log.error("请求盘古-查询会议列表异常",e);
        }
        return null;

    }


    /**
     * 查询客户信息
     * @param request
     * @return
     */
    public String queryCustomers(BaseParmDto<QueryCustomersReqDto> request){
        try {
            JSONObject object = new JSONObject();
            object.put("PageIndex",request.getPageIndex());
            object.put("PageSize",request.getPageSize());
            object.put("OrderBy","");
            object.put("SortCol","");
            JSONObject where = new JSONObject();
            where.put("param",request.getParam().getParam());
            where.put("intentionGrade","");
            where.put("saleId",request.getParam().getSaleId());
            where.put("meetingNo",request.getParam().getMeetingNo());
            object.put("Where",where);
            return callPanguApi(object,getSaleCustomerListUrl);
        } catch (Exception e) {
            log.error("请求盘古-查询会议列表异常",e);
        }
        return null;
    }


    /**
     * 发送邀请函
     * @return
     */
    public String sendInvitation(SendInvitationReqDto request){
        try {
            JSONObject object = new JSONObject();
            object.put("opreator",request.getOpreator());
            object.put("clueGuid",request.getClueGuid());
            object.put("meetingNo",request.getCode());
            object.put("isSendShort",request.getIsSendShort());
            return callPanguApi(object,addInviteInfoUrl);
        } catch (Exception e) {
            log.error("请求盘古-发送邀请函异常",e);
        }
        return null;
    }


    /**
     *查询销售人员信息
     * @param userId
     * @return
     */
    public String querySalesInfo(String userId){
        try {
            return getCallPanguApi(new HashMap(){{put("userId",userId);}},querySalesInfoUrl);
        } catch (Exception e) {
            log.error("请求盘古-查询销售人员信息异常",e);
        }
        return null;
    }


    /**
     * 转交客户
     * @param userId
     * @param cluesIds
     * @param saleIds
     * @return
     */
    public String handOverCustomer(String userId, List<Long> cluesIds, List<Integer> saleIds){
        try {
            JSONObject object = new JSONObject();
            object.put("UserId",userId);
            object.put("CluesIds",cluesIds);
            object.put("UserIds",saleIds);
            return callPanguApi(object,handOverCustomerUrl);
        } catch (Exception e) {
            log.error("请求盘古-转交客户异常",e);
        }
        return null;

    }


    /**
     * 新增沟通记录
     * @param request
     * @return
     */
    public String addTalkNotes(TalkNotesReqDto request){
        try {
            JSONObject object = new JSONObject();
            object.put("UserId",request.getUserId());
            object.put("ClueId",request.getCustomerId());
            object.put("Intention",request.getIntention());
            object.put("NextTalkTime",request.getNextTalkTime());
            object.put("Questions",request.getQuestions());
            object.put("Pictures",request.getImgUrl());
            object.put("TalkRecord",request.getTalkRecord());
            return callPanguApi(object,addTalkNotesUrl);
        } catch (Exception e) {
            log.error("请求盘古-新增沟通记录异常",e);
        }
        return null;
    }


    /**
     * 获取客户历史沟通记录
     * @param clueGuid
     * @return
     */
    public String getCommunicateList(Long clueGuid){

        try {
            JSONObject object = new JSONObject();
            object.put("ClueGuid",clueGuid);
            return callPanguApi(object,getCommunicateListUrl);
        } catch (Exception e) {
            log.error("请求盘古-获取客户历史沟通记录",e);
        }
        return null;

    }





}
